package com.util.logging;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.ResourceBundle;
import java.util.logging.FileHandler;
import java.util.logging.Filter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.util.logging.XMLFormatter;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Aleksandar
 */
public class JLogger {

    private static List<Logger_> loggerHolder = new ArrayList<Logger_>(2);
    private static FileHandler fh;

    static {
        try {
            String fileName = "src" + File.separator + "log_test.txt";
            fh = new FileHandler(fileName, new File(fileName).exists());
        } catch (IOException ex) {
            Logger.getLogger(JLogger.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(JLogger.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static JLogger getLogger(String name) {
        Logger logger = Logger.getLogger(name);
//        fh.setFormatter(new XMLFormatter());
        LogManager.getLogManager().addLogger(logger);
        logger.setLevel(Level.INFO);
        logger.setUseParentHandlers(false);
//        fh.setFormatter(new HttpLogFormatter());
        fh.setFormatter(new SimpleFormatter());
//        fh.setFormatter(new XMLFormatter());
        logger.addHandler(fh);
        JLogger jlogger = new JLogger();
        loggerHolder.add(new Logger_(jlogger, logger));
        return jlogger;
    }

    public void log(Level level, String test_1) {
        getCurrentLogger().log(level, test_1);
    }

    public ResourceBundle getResourceBundle() {
        return getCurrentLogger().getResourceBundle(); //To change body of generated methods, choose Tools | Templates.
    }

    public String getResourceBundleName() {
        return getCurrentLogger().getResourceBundleName(); //To change body of generated methods, choose Tools | Templates.
    }

    public void setFilter(Filter newFilter) throws SecurityException {
        getCurrentLogger().setFilter(newFilter); //To change body of generated methods, choose Tools | Templates.
    }

    public Filter getFilter() {
        return getCurrentLogger().getFilter(); //To change body of generated methods, choose Tools | Templates.
    }

    public void log(LogRecord record) {
        getCurrentLogger().log(record); //To change body of generated methods, choose Tools | Templates.
    }

    public void log(Level level, String msg, Object param1) {
        getCurrentLogger().log(level, msg, param1); //To change body of generated methods, choose Tools | Templates.
    }

    public void log(Level level, String msg, Object[] params) {
        getCurrentLogger().log(level, msg, params); //To change body of generated methods, choose Tools | Templates.
    }

    public void log(Level level, String msg, Throwable thrown) {
        getCurrentLogger().log(level, msg, thrown); //To change body of generated methods, choose Tools | Templates.
    }

    public void logp(Level level, String sourceClass, String sourceMethod, String msg) {
        getCurrentLogger().logp(level, sourceClass, sourceMethod, msg); //To change body of generated methods, choose Tools | Templates.
    }

    public void logp(Level level, String sourceClass, String sourceMethod, String msg, Object param1) {
        getCurrentLogger().logp(level, sourceClass, sourceMethod, msg, param1); //To change body of generated methods, choose Tools | Templates.
    }

    public void logp(Level level, String sourceClass, String sourceMethod, String msg, Object[] params) {
        getCurrentLogger().logp(level, sourceClass, sourceMethod, msg, params); //To change body of generated methods, choose Tools | Templates.
    }

    public void logp(Level level, String sourceClass, String sourceMethod, String msg, Throwable thrown) {
        getCurrentLogger().logp(level, sourceClass, sourceMethod, msg, thrown); //To change body of generated methods, choose Tools | Templates.
    }

    public void logrb(Level level, String sourceClass, String sourceMethod, String bundleName, String msg) {
        getCurrentLogger().logrb(level, sourceClass, sourceMethod, bundleName, msg); //To change body of generated methods, choose Tools | Templates.
    }

    public void logrb(Level level, String sourceClass, String sourceMethod, String bundleName, String msg, Object param1) {
        getCurrentLogger().logrb(level, sourceClass, sourceMethod, bundleName, msg, param1); //To change body of generated methods, choose Tools | Templates.
    }

    public void logrb(Level level, String sourceClass, String sourceMethod, String bundleName, String msg, Object[] params) {
        getCurrentLogger().logrb(level, sourceClass, sourceMethod, bundleName, msg, params); //To change body of generated methods, choose Tools | Templates.
    }

    public void logrb(Level level, String sourceClass, String sourceMethod, String bundleName, String msg, Throwable thrown) {
        getCurrentLogger().logrb(level, sourceClass, sourceMethod, bundleName, msg, thrown); //To change body of generated methods, choose Tools | Templates.
    }

    public void entering(String sourceClass, String sourceMethod) {
        getCurrentLogger().entering(sourceClass, sourceMethod); //To change body of generated methods, choose Tools | Templates.
    }

    public void entering(String sourceClass, String sourceMethod, Object param1) {
        getCurrentLogger().entering(sourceClass, sourceMethod, param1); //To change body of generated methods, choose Tools | Templates.
    }

    public void entering(String sourceClass, String sourceMethod, Object[] params) {
        getCurrentLogger().entering(sourceClass, sourceMethod, params); //To change body of generated methods, choose Tools | Templates.
    }

    public void exiting(String sourceClass, String sourceMethod) {
        getCurrentLogger().exiting(sourceClass, sourceMethod); //To change body of generated methods, choose Tools | Templates.
    }

    public void exiting(String sourceClass, String sourceMethod, Object result) {
        getCurrentLogger().exiting(sourceClass, sourceMethod, result); //To change body of generated methods, choose Tools | Templates.
    }

    public void throwing(String sourceClass, String sourceMethod, Throwable thrown) {
        getCurrentLogger().throwing(sourceClass, sourceMethod, thrown); //To change body of generated methods, choose Tools | Templates.
    }

    public void severe(String msg) {
        getCurrentLogger().severe(msg); //To change body of generated methods, choose Tools | Templates.
    }

    public void warning(String msg) {
        getCurrentLogger().warning(msg); //To change body of generated methods, choose Tools | Templates.
    }

    public void info(String msg) {
        getCurrentLogger().info(msg); //To change body of generated methods, choose Tools | Templates.
    }

    public void config(String msg) {
        getCurrentLogger().config(msg); //To change body of generated methods, choose Tools | Templates.
    }

    public void fine(String msg) {
        getCurrentLogger().fine(msg); //To change body of generated methods, choose Tools | Templates.
    }

    public void finer(String msg) {
        getCurrentLogger().finer(msg); //To change body of generated methods, choose Tools | Templates.
    }

    public void finest(String msg) {
        getCurrentLogger().finest(msg); //To change body of generated methods, choose Tools | Templates.
    }

    public void setLevel(Level newLevel) throws SecurityException {
        getCurrentLogger().setLevel(newLevel); //To change body of generated methods, choose Tools | Templates.
    }

    public Level getLevel() {
        return getCurrentLogger().getLevel(); //To change body of generated methods, choose Tools | Templates.
    }

    public boolean isLoggable(Level level) {
        return getCurrentLogger().isLoggable(level); //To change body of generated methods, choose Tools | Templates.
    }

    public String getName() {
        return getCurrentLogger().getName(); //To change body of generated methods, choose Tools | Templates.
    }

    public void addHandler(Handler handler) throws SecurityException {
        getCurrentLogger().addHandler(handler); //To change body of generated methods, choose Tools | Templates.
    }

    public void removeHandler(Handler handler) throws SecurityException {
        getCurrentLogger().removeHandler(handler); //To change body of generated methods, choose Tools | Templates.
    }

    public Handler[] getHandlers() {
        return getCurrentLogger().getHandlers(); //To change body of generated methods, choose Tools | Templates.
    }

    public void setUseParentHandlers(boolean useParentHandlers) {
        getCurrentLogger().setUseParentHandlers(useParentHandlers); //To change body of generated methods, choose Tools | Templates.
    }

    public boolean getUseParentHandlers() {
        return getCurrentLogger().getUseParentHandlers(); //To change body of generated methods, choose Tools | Templates.
    }

    public Logger getParent() {
        return getCurrentLogger().getParent(); //To change body of generated methods, choose Tools | Templates.
    }

    public void setParent(Logger parent) {
        getCurrentLogger().setParent(parent); //To change body of generated methods, choose Tools | Templates.
    }

    protected Logger getCurrentLogger() {
        return loggerHolder.get(loggerHolder.indexOf(new Logger_(this, null))).logger;
    }
}

class Logger_ {

    Logger logger;
    JLogger jlogger;

    public Logger_() {
    }

    public Logger_(JLogger jlogger, Logger logger) {
        this.logger = logger;
        this.jlogger = jlogger;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Logger_ other = (Logger_) obj;
        if (!Objects.equals(this.jlogger, other.jlogger)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 37 * hash + Objects.hashCode(this.jlogger);
        return hash;
    }
}